package com.zwr.sheller.core.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * @Author LSY
 * @Date 2021/11/29 16:37
 * @Version 1.0
 **/
public class JsonUtil {

    private static final ObjectMapper objectMapper;

    static {
        objectMapper = new ObjectMapper();
    }

    public static String objectToString(Object object) {
        String str = null;
        try {
            str = objectMapper.writeValueAsString(object);
        }
        catch (JsonProcessingException e) {
            // 工具类的异常还是需要记录到日志文件中
            e.printStackTrace();
        }
        return str;
    }

    public static <T> T stringToObject(String str, Class<T> clazz) {
        T t = null;
        try {
            t = objectMapper.readValue(str, clazz);
        }
        catch (JsonProcessingException e) {
            // 工具类的异常还是需要记录到日志文件中
            e.printStackTrace();
        }
        return t;
    }
}
